package com.jason.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.jason.system.common.ResultEntity;
import com.jason.system.common.util.EasyPoiUtil;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.SolveOrder;
import com.jason.system.entity.WorkLoad;
import com.jason.system.service.WorkLoadService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/4/23 14:57
 */
@RestController
@RequestMapping("/api/workLoad")
@Api(value = "/workLoad",tags = "工作量统计接口")
public class WorkLoadController {

  private static final Logger logger = LoggerFactory.getLogger(WorkLoadController.class);

  @Autowired
  private WorkLoadService workLoadService;

  @ApiOperation(value = "获取学生所有工作量",notes = "获取每个学生所有工作量")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "page",value = "页数",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "size",value = "每页个数",required = true,dataType = "Integer")
  })
  @ApiResponses({
          @ApiResponse(code = 200, message = "获取学生所有工作量成功"),
          @ApiResponse(code = 201, message = "获取学生所有工作量失败")
  })
  @GetMapping("/getAll")
  public JSONObject getAll(@RequestParam("page") Integer page, @RequestParam("size") Integer size){
    logger.info("==>>>WorkLoadController==>>>进行路径：/api/workLoad/getAll.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    logger.info("==>>>WorkLoadController==>>>正在获取工作量.....");
    Pager<WorkLoad> workLoadPager = workLoadService.getAllWorkLoad(page,size);
    if (workLoadPager!=null){
      logger.info("==>>>WorkLoadController==>>>获取学生所有工作量成功.....");
      r.setCode(200);
      r.setMessage("获取学生所有工作量信息成功");
      r.setData(workLoadPager);
    }else {
      logger.info("==>>>WorkLoadController==>>>获取学生所有工作量失败.....");
      r.setCode(201);
      r.setMessage("获取学生所有工作量信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }


  @ApiOperation(value = "根据时间段获取学生工作量",notes = "根据时间段获取所有学生工作量")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "query",name = "page",value = "页数",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "size",value = "每页个数",required = true,dataType = "Integer"),
          @ApiImplicitParam(paramType = "query",name = "startTime",value = "开始时间",required = true,dataType = "Date"),
          @ApiImplicitParam(paramType = "query",name = "endTime",value = "结束时间",required = true,dataType = "Date")
  })
  @ApiResponses({
          @ApiResponse(code = 200, message = "根据时间段获取学生所有工作量成功"),
          @ApiResponse(code = 201, message = "根据时间段获取学生所有工作量失败")
  })
  @PostMapping("/getAllByTime")
  public JSONObject getAllByTime(Integer page,Integer size, String startTime, String endTime) throws ParseException {
    logger.info("==>>>WorkLoadController==>>>进行路径：/api/workLoad/getAllByTime.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Pager<WorkLoad> workLoadPager = workLoadService.getAllWorkLoadByTime(page,size,sdf.parse(startTime),sdf.parse(endTime));
    if (workLoadPager!=null){
      logger.info("==>>>WorkLoadController==>>>根据时间段获取学生所有工作量成功.....");
      r.setCode(200);
      r.setMessage("根据时间段获取学生所有工作量信息成功");
      r.setData(workLoadPager);
    }else {
      logger.info("==>>>WorkLoadController==>>>根据时间段获取学生所有工作量失败.....");
      r.setCode(201);
      r.setMessage("根据时间段获取学生所有工作量信息失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }

  @ApiOperation(value = "将学生工作量导入excel中",notes = "将所有学生工作量导入至excel中")
  @ApiImplicitParams({
          @ApiImplicitParam(paramType = "body",name = "workLoadList",value = "工作量信息列表",required = true,dataType = "List"),
          @ApiImplicitParam(paramType = "body",name = "time",value = "当前时间",required = true,dataType = "Date")
  })
  @ApiResponses({
          @ApiResponse(code = 200, message = "导出学生工作量至excel成功"),
          @ApiResponse(code = 201, message = "导出学生工作量至excel失败")
  })
  @PostMapping("/downLoad")
  public JSONObject downLoad(HttpServletResponse response, List<WorkLoad> workLoadList, Date time){
    logger.info("==>>>WorkLoadController==>>>进行路径：/api/workLoad/downLoad.....");
    JSONObject jsonObject = new JSONObject();
    ResultEntity r = new ResultEntity();
    //获取月份信息
    Calendar cld = Calendar.getInstance();
    cld.setTime(time);
    Integer month = cld.get(Calendar.MONTH);
    String fileName = "同学"+month+"月个人工作量汇总";
    try {
      logger.info("==>>>WorkLoadController==>>>使用easyPoi工具类将同学工作量信息导入至excel中...");
      EasyPoiUtil.defaultExport(workLoadList,fileName,"workLoad",fileName+".xlsx",
              SolveOrder.class,response);
      r.setCode(200);
      r.setMessage("导入至excel中成功");
    }catch (Exception e){
      logger.info("==>>>WorkLoadController==>>>使用easyPoi工具类将同学工作量信息导入至excel失败:{}",e.getMessage());
      r.setCode(201);
      r.setMessage("导入至excel中失败");
    }
    jsonObject.put("result",r);
    return jsonObject;
  }
}
